<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="所属组织" prop="signingDept">
        <treeselect
          appendToBody
          z-index="999999"
          :disabled="disabled"
          v-model="queryParams.signingDept"
          :options="deptList"
          :normalizer="normalizer"
          :default-expand-level="100"
          style="width:306px"
          placeholder="请选择签约单位"
        />
      </el-form-item>
      <el-form-item label="" prop="projectManager">
        <el-input
          v-model="queryParams.search"
          placeholder="请输入项目名称、工程地址、项目经理、项目编码、项目经理电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="承包模式" prop="contractingModel">
        <el-select
          v-model="queryParams.contractingModel"
          placeholder="承包模式:全部"
          clearable
        >
          <el-option
            v-for="dict in dict.type.common_contracting_model"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['common:project:add']"
          >新增项目</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['common:project:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['common:project:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="projectList">
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="编码" align="center" prop="code" />
      <el-table-column label="项目经理" align="center" prop="projectManager" />
      <el-table-column label="手机" align="center" prop="phone" />
      <el-table-column label="行业类别" align="center" prop="industryCategory">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.common_industry_category"
            :value="scope.row.industryCategory"
          />
        </template>
      </el-table-column>
      <el-table-column label="项目状态" align="center" prop="projectStatus">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.common_project_status"
            :value="scope.row.projectStatus"
          />
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="LookUpdate(scope.row)"
            v-hasPermi="['common:project:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['common:project:edit']"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="assignment(scope.row)"
            v-hasPermi="['common:user:list']"
            >分配人员</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['common:project:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改项目管理对话框 -->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="open"
      width="1200px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-tabs>
          <el-tab-pane label="基本信息">
            <el-col :span="8"
              ><el-form-item label="名称" prop="name">
                <el-input
                  :disabled="disabled"
                  v-model="form.name"
                  placeholder="请输入名称"
                /> </el-form-item
            ></el-col>
            <el-col :span="8">
              <el-form-item label="编码" prop="code">
                <el-input
                  :disabled="disabled"
                  v-model="form.code"
                  placeholder="请输入编码"
                /> </el-form-item
            ></el-col>
            <el-col :span="8"
              ><el-form-item label="简称" prop="abbreviation">
                <el-input
                  :disabled="disabled"
                  v-model="form.abbreviation"
                  placeholder="请输入简称"
                /> </el-form-item
            ></el-col>
          </el-tab-pane>
        </el-tabs>
        <el-tabs>
          <el-tab-pane label="项目信息">
            <el-row>
              <el-col :span="8">
                <el-form-item label="签约单位" prop="signingDept">
                  <treeselect
                    appendToBody
                    z-index="999999"
                    :disabled="disabled"
                    v-model="form.signingDept"
                    :options="deptList"
                    :normalizer="normalizer"
                    :default-expand-level="100"
                    placeholder="请选择签约单位"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="项目经理" prop="projectManager">
                  <el-input
                    :disabled="disabled"
                    v-model="form.projectManager"
                    placeholder="请输入项目经理"
                  /> </el-form-item
              ></el-col>
              <el-col :span="8">
                <el-form-item label="手机号" prop="phone">
                  <el-input
                    :disabled="disabled"
                    v-model="form.phone"
                    placeholder="请输入手机号"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="承包模式" prop="contractingModel">
                  <el-select
                    style="width: 266.66px"
                    :disabled="disabled"
                    v-model="form.contractingModel"
                    placeholder="请选择承包模式"
                  >
                    <el-option
                      v-for="dict in dict.type.common_contracting_model"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="项目等级" prop="projectLevel">
                  <el-select
                    style="width: 266.66px"
                    v-model="form.projectLevel"
                    placeholder="请选择项目等级"
                    :disabled="disabled"
                  >
                    <el-option
                      :disabled="disabled"
                      v-for="dict in dict.type.common_project_level"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="所属父项目" prop="parentCode">
                  <el-input
                    :disabled="disabled"
                    v-model="form.parentCode"
                    placeholder="请输入所属父项目"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="是否集团名义" prop="whetherBloc">
                  <el-radio-group
                    :disabled="disabled"
                    v-model="form.whetherBloc"
                  >
                    <el-radio
                      v-for="dict in dict.type.common_whether_bloc"
                      :key="dict.value"
                      :label="dict.value"
                      >{{ dict.label }}</el-radio
                    >
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="施工许可证" prop="licenses">
                  <el-tag
                    :key="tag"
                    v-for="tag in dynamicTags"
                    :closable="!disabled"
                    :disable-transitions="false"
                    @close="handleClose(tag)"
                  >
                    {{ tag }}
                  </el-tag>
                  <el-input
                    :disabled="disabled"
                    class="input-new-tag"
                    v-if="inputVisible"
                    v-model="inputValue"
                    ref="saveTagInput"
                    size="small"
                    @keyup.enter.native="handleInputConfirm"
                    @blur="handleInputConfirm"
                  >
                  </el-input>
                  <el-button
                    :disabled="disabled"
                    v-else
                    class="button-new-tag"
                    size="small"
                    @click="showInput"
                  >
                    + 添加施工许可证</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <el-tabs>
          <el-tab-pane label="工程信息">
            <el-row>
              <el-col :span="8"
                ><el-form-item label="行业类别" prop="industryCategory">
                  <el-select
                    style="width: 266.66px"
                    :disabled="disabled"
                    v-model="form.industryCategory"
                    placeholder="请选择行业类别"
                  >
                    <el-option
                      v-for="dict in dict.type.common_industry_category"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="工程造价" prop="projectPrice">
                  <el-input
                    :disabled="disabled"
                    v-model="form.projectPrice"
                    placeholder="请输入工程造价"
                  /> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="项目状态" prop="projectStatus">
                  <el-select
                    style="width: 266.66px"
                    :disabled="disabled"
                    v-model="form.projectStatus"
                    placeholder="请选择项目状态"
                  >
                    <el-option
                      v-for="dict in dict.type.common_project_status"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    ></el-option>
                  </el-select> </el-form-item
              ></el-col>
            </el-row>
            <el-row>
              <el-col :span="8"
                ><el-form-item label="工程地址" prop="projectAddress">
                  <el-input
                    :disabled="disabled"
                    v-model="form.projectAddress"
                    placeholder="请输入工程地址"
                  /> </el-form-item
              ></el-col>
              <el-col :span="16"
                ><el-form-item label="项目概况" prop="projectOverview">
                  <el-input
                    :disabled="disabled"
                    v-model="form.projectOverview"
                    placeholder="请输入项目概况"
                  /> </el-form-item
              ></el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <el-tabs>
          <el-tab-pane label="合同信息">
            <el-row>
              <el-col :span="8"
                ><el-form-item label="合同签订日期" prop="signingDate">
                  <el-date-picker
                    style="width: 266.66px"
                    :disabled="disabled"
                    clearable
                    v-model="form.signingDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择合同签订日期"
                  >
                  </el-date-picker> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="合同开工日期" prop="startDate">
                  <el-date-picker
                    style="width: 266.66px"
                    :disabled="disabled"
                    clearable
                    v-model="form.startDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择合同开工日期"
                  >
                  </el-date-picker> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="合同竣工日期" prop="endDate">
                  <el-date-picker
                    style="width: 266.66px"
                    :disabled="disabled"
                    clearable
                    v-model="form.endDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择合同竣工日期"
                  >
                  </el-date-picker> </el-form-item
              ></el-col>
            </el-row>
            <el-row>
              <el-col :span="8">
                <el-form-item label="合同工期" prop="dateCount">
                  <el-input
                    :disabled="disabled"
                    v-model="form.dateCount"
                    placeholder="请输入合同工期"
                  /> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="实际开工日期" prop="realStartDate">
                  <el-date-picker
                    style="width: 266.66px"
                    :disabled="disabled"
                    clearable
                    v-model="form.realStartDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择实际开工日期"
                  >
                  </el-date-picker> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="实际竣工日期" prop="realEndDate">
                  <el-date-picker
                    style="width: 266.66px"
                    :disabled="disabled"
                    clearable
                    v-model="form.realEndDate"
                    type="date"
                    value-format="yyyy-MM-dd"
                    placeholder="请选择实际竣工日期"
                  >
                  </el-date-picker> </el-form-item
              ></el-col>
            </el-row>
            <el-row>
              <el-col :span="8"
                ><el-form-item label="实际工期" prop="realDateCount">
                  <el-input
                    :disabled="disabled"
                    v-model="form.realDateCount"
                    placeholder="请输入实际工期"
                  /> </el-form-item
              ></el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
        <el-tabs>
          <el-tab-pane label="中标单位信息">
            <el-row>
              <el-col :span="8"
                ><el-form-item label="施工单位" prop="constructionDept">
                  <el-input
                    :disabled="disabled"
                    v-model="form.constructionDept"
                    placeholder="请输入施工单位"
                  /> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="建设单位" prop="buildDept">
                  <el-input
                    :disabled="disabled"
                    v-model="form.buildDept"
                    placeholder="请输入建设单位"
                  /> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="设计单位" prop="designDept">
                  <el-input
                    :disabled="disabled"
                    v-model="form.designDept"
                    placeholder="请输入设计单位"
                  /> </el-form-item
              ></el-col>
            </el-row>
            <el-row>
              <el-col :span="8"
                ><el-form-item label="监理单位" prop="supervisionDept">
                  <el-input
                    :disabled="disabled"
                    v-model="form.supervisionDept"
                    placeholder="请输入监理单位"
                  /> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="业主单位" prop="ownerDept">
                  <el-input
                    :disabled="disabled"
                    v-model="form.ownerDept"
                    placeholder="请输入业主单位"
                  /> </el-form-item
              ></el-col>
              <el-col :span="8"
                ><el-form-item label="勘察单位" prop="surveyDept">
                  <el-input
                    :disabled="disabled"
                    v-model="form.surveyDept"
                    placeholder="请输入勘察单位"
                  /> </el-form-item
              ></el-col>
            </el-row>
            <el-row>
              <el-col :span="8"
                ><el-form-item label="发包单位" prop="outsourcingDept">
                  <el-input
                    :disabled="disabled"
                    v-model="form.outsourcingDept"
                    placeholder="请输入发包单位"
                  /> </el-form-item
              ></el-col>
              <el-col :span="8"></el-col>
              <el-col :span="8"></el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProject,
  getProject,
  delProject,
  addProject,
  updateProject,
} from "@/api/common/project";
import { listDept } from "@/api/system/dept";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Project",
  dicts: [
    "common_contracting_model",
    "common_project_level",
    "common_project_status",
    "common_whether_bloc",
    "common_industry_category",
  ],
  components: { Treeselect },
  data() {
    return {
      // 树数据
      deptList: [],
      //查看
      disabled: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目管理表格数据
      projectList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        signingDept: null,
        search: null,
        signingDept:null,
        contractingModel: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
      },
      dynamicTags: [],
      inputVisible: false,
      inputValue: "",
    };
  },
  created() {
    this.getList();
    this.getTreeList();
  },
  methods: {
    /** 查询项目管理列表 */
    getList() {
      this.loading = true;
      listProject(this.queryParams).then((response) => {
        this.projectList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询树列表 */
    getTreeList() {
      listDept().then((response) => {
        this.deptList = this.handleTree(response.data, "deptId");
      });
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      };
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.dynamicTags = [];
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        name: null,
        code: null,
        abbreviation: null,
        signingDept: null,
        projectManager: null,
        phone: null,
        contractingModel: null,
        projectLevel: null,
        parentCode: null,
        whetherBloc: null,
        license: null,
        industryCategory: null,
        projectPrice: null,
        projectStatus: null,
        projectAddress: null,
        projectOverview: null,
        signingDate: null,
        startDate: null,
        endDate: null,
        dateCount: null,
        realStartDate: null,
        realEndDate: null,
        realDateCount: null,
        constructionDept: null,
        buildDept: null,
        designDept: null,
        supervisionDept: null,
        ownerDept: null,
        surveyDept: null,
        outsourcingDept: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        search: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      (this.dynamicTags = []), (this.disabled = false);
      this.open = true;
      this.title = "添加项目管理";
    },
    //查看
    LookUpdate(row) {
      this.reset();
      this.dynamicTags = [];
      const id = row.id;
      getProject(id).then((response) => {
        this.form = response.data;
        this.dynamicTags = response.data.licenses;
        this.disabled = true;
        this.open = true;
        this.title = "查看项目管理";
      });
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.dynamicTags = [];
      const id = row.id || this.ids;
      getProject(id).then((response) => {
        this.form = response.data;
        this.dynamicTags = response.data.licenses;
        this.disabled = false;
        this.open = true;
        this.title = "修改项目管理";
      });
    },
    //分配角色
    assignment(row) {
      this.$router.push({
        name: "ProjectUser",
        query: {
          id: row.id,
          name: row.name,
          projectManager: row.projectManager,
          projectStatus: row.projectStatus,
        },
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateProject(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProject(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除项目管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delProject(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "common/project/export",
        {
          ...this.queryParams,
        },
        `project_${new Date().getTime()}.xlsx`
      );
    },

    handleClose(tag) {
      this.dynamicTags.splice(this.dynamicTags.indexOf(tag), 1);
    },

    showInput() {
      this.inputVisible = true;
      this.$nextTick((_) => {
        this.$refs.saveTagInput.$refs.input.focus();
      });
    },

    handleInputConfirm() {
      let inputValue = this.inputValue;
      if (inputValue) {
        this.dynamicTags.push(inputValue);
      }
      this.inputVisible = false;
      this.inputValue = "";
      this.form.licenses = this.dynamicTags;
    },
  },
};
</script>
<style scoped></style>
